/**
 * 
 */
package com.iwords.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author       BOLIN.HBC 
 * @time         2011-5-1    
 * @description  ZIP-Tools       
 */
public class ZipTools {

	private static final Log log = LogFactory.getLog(ZipTools.class);

	/**
	 * 压缩
	 * 
	 * @param bytes
	 */
	public static void compress(byte[] bytes) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ZipOutputStream zout = new ZipOutputStream(bos);
		try {
			zout.write(bytes);
		} catch (IOException e) {
			log.error(e);
		} finally {
			try {
				bos.close();
				zout.close();
			} catch (IOException e) {
				log.error(e);
			}

		}
	}

	 
	/**
	 * 解压
	 * 
	 * @param bytes
	 */
	public static void unCompress(byte[] bytes) {
		ByteArrayInputStream in = new ByteArrayInputStream(bytes);
		ZipInputStream zin = new ZipInputStream(in);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			zin.getNextEntry();
		} catch (IOException e) {
			log.error(e);
		}
		byte[] buffer = new byte[bytes.length];
		int offset = -1;
		try {
			while ((offset = zin.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
		} catch (IOException e) {

			log.error(e);
		} finally {
			try {
				in.close();
				zin.close();
				out.close();
			} catch (IOException e) {
				log.error(e);
			}

		}
	}

}
